#include<windows.h>
#include<iostream>
#include<ctime>
#include<conio.h>
#include<cstdlib>
#include<stdio.h>

#define WIDE 50
#define HIGH 20
#define STNUM 30

using namespace std;

char x[4] = "■";

//移动光标函数，x从左往右，y从上往下；
void gotoxy(int x, int y)
{
	COORD position;
	position.X = x;
	position.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
//打印边框函数，矩形的左上角（ix，iy），右下角（jx，jy）；
void prboard(int ix, int iy, int jx, int jy, char r[])
{
	for (int x = ix; x <= jx; x += 2)
	{
		gotoxy(x, iy);
		cout << r;
		gotoxy(x, jy);
		cout << r;
	}
	for (int y = iy; y <= jy; y++)
	{
		gotoxy(ix, y);
		cout << r;
		gotoxy(jx, y);
		cout << r;
	}
}
//清屏函数
void clear(int ix, int iy, int jx, int jy)
{
	for (int x = ix; x <= jx; x++)
	{
		for (int y = iy; y <= jy; y++)
		{
			gotoxy(x, y);
			cout << "  ";
		}
	}
}

void begin()
{
	prboard(0,0,WIDE,HIGH,x);
	gotoxy(WIDE / 2 - 6, HIGH / 2);
	cout << "简易抽签程序";
	gotoxy(2, HIGH - 1);
	cout << "z抽签";
	gotoxy(WIDE - 7, HIGH - 1);
	cout << "x退出";
	gotoxy(WIDE - 2, 0);
}

bool doit()
{
	int point=0;
	clear(WIDE / 2 - 6, HIGH / 2, WIDE / 2 + 6, HIGH / 2);
	gotoxy(WIDE / 2 - 8, HIGH / 2 - 1);
	cout << "幸运儿是:";
	gotoxy(WIDE / 2 + 1, HIGH / 2);
	cout << " 号!";
	srand(time(0));//与时间相关的一个种子
	while (1)
	{
		point = (rand() % STNUM) + 1;
		char key1 = 0;
		clear(WIDE / 2 - 2, HIGH / 2, WIDE / 2, HIGH / 2);
		gotoxy(WIDE / 2 - 2, HIGH / 2);
		printf("%2.d",point );
		gotoxy(WIDE - 1, 0);
		Sleep(50);
		if (_kbhit())//如果键盘输入
		{
			key1 = getch();
			cin.sync();
			if (key1 == 'z') break;
			else if (key1 == 'x') return false;
		}
	}
	return true;
}

int main()
{
	bool key=true;
	begin();//开始界面
	do
	{
	    while (true)
	    {
	    int ch=getch();
		if (ch == 'x') return 0;
		else if(ch=='z') break;
		cin.sync();
	    }
		key=doit();
	} while (key);
	return 0;
}
